package com.qianli.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.utils.MD5;
import com.fqgj.xjd.user.dao.UserAccessDao;
import com.fqgj.xjd.user.dao.UserAuthBankDao;
import com.fqgj.xjd.user.dao.UserAuthStatusDao;
import com.fqgj.xjd.user.dao.UserDao;
import com.fqgj.xjd.user.entity.UserAccessEntity;
import com.fqgj.xjd.user.entity.UserAuthStatusEntity;
import com.fqgj.xjd.user.entity.UserEntity;
import com.qianli.user.domain.model.auth.Customer;
import com.qianli.user.enums.UserAuthEnum;
import com.qianli.user.enums.UserAuthStatusEnum;
import com.qianli.user.ro.auth.UserAuthStatusRO;
import com.qianli.user.service.UserAuthService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * name:认证查询服务
 * auth:ting.li@qianli-inc.com
 * date:2018/2/22 17:06
 * version: v1.0.0
 **/
@Service
public class UserAuthServiceImpl implements UserAuthService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserAuthService.class);

    @Autowired
    private UserAuthBankDao userAuthBankDao;

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserAccessDao userAccessDao;
    @Autowired
    private UserAuthStatusDao userAuthStatusDao;

    /**
     * 该卡是否被该平台其他用户绑定
     *
     * @param cardNo
     * @param biz
     * @param appCode
     * @return
     */
    @Override
    public Boolean isBankBinded(String userCode, String cardNo, Integer biz, String appCode) {

        String preUserCode = userAuthBankDao.isBankBinded(cardNo, biz, appCode);
        if (StringUtils.isEmpty(preUserCode) || userCode.equals(preUserCode)) {
            return false;
        }
        return true;
    }

    /**
     * 实名认证服务
     *
     * @param customer
     */
    @Override
    public void authCustomer(Customer customer) {
        UserEntity userEntity = userDao.selectByCustomerCode(customer.getCustomerCode());

        //if (userEntity != null
        //        && userEntity.getIdentityNo().equals(customer.getIdentityNo())
        //        && !userEntity.getName().equals(customer.getName())) {
        //    throw new UserException(UserStateCode.USER_REALNAME_NOT_MATCH_WITH_PRE);
        //}
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(customer.getUserCode());
        LOGGER.info("authCustomer customer:{}", JSON.toJSON(customer));
        if (userEntity == null && userAccessEntity != null) {
            UserEntity newCustomerEntity = new UserEntity();
            newCustomerEntity.setCustomerCode(customer.getCustomerCode());
            newCustomerEntity.setUserCode(customer.getUserCode());
            newCustomerEntity.setName(customer.getName());
            newCustomerEntity.setIdentityNo(customer.getIdentityNo());
            newCustomerEntity.setMobile(userAccessEntity.getMobile());
            newCustomerEntity.setMd5Mobile(MD5.md5(userAccessEntity.getMobile()));
            newCustomerEntity.setMd5MobileIdentityNo(MD5.md5(userAccessEntity.getMobile() + customer.getIdentityNo()));
            userDao.insert(newCustomerEntity);
            userAccessDao.updateCustomerCodeByUserCode(customer.getUserCode(), customer.getUserCode());
        }
    }

    @Override
    public Boolean updateUserAuthStatus(String userCode, UserAuthStatusEnum userAuthStatusEnum, UserAuthEnum userAuthEnum) {
        UserAuthStatusEntity userAuthStatusEntity = userAuthStatusDao.getUserAuthStatusByUserCode(userCode);
        if(userAuthStatusEntity == null){
            return Boolean.FALSE;
        }
        UserAuthStatusEntity entity = new UserAuthStatusEntity();
        BeanUtils.copyProperties(userAuthStatusEntity, entity);
        switch (userAuthEnum){
            case BASE_INFO_STATUS:
                entity.setBaseInfoStatus(userAuthStatusEnum.getCode());
                break;
            case BASE_WORK_STATUS:
                entity.setBaseWorkStatus(userAuthStatusEnum.getCode());
                break;
            case BASE_EMERGENCY_STATUS:
                entity.setBaseEmergencyStatus(userAuthStatusEnum.getCode());
                break;
            case ADVANCE_STATUS:
                entity.setAdvanceAadhaarStatus(userAuthStatusEnum.getCode());
                break;
            case AADHAAR_STATUS:
                entity.setAadhaarStatus(userAuthStatusEnum.getCode());
                break;
            case VOTER_STATUS:
                entity.setVoterStatus(userAuthStatusEnum.getCode());
                break;
            case PASSPORT_STATUS:
                entity.setPassportStatus(userAuthStatusEnum.getCode());
                break;
            case DRIVER_LICENSE_STATUS:
                entity.setDriverLicenseStatus(userAuthStatusEnum.getCode());
                break;
            case PAN_STATUS:
                entity.setPanStatus(userAuthStatusEnum.getCode());
                break;
            case BANK_STATUS:
                entity.setBankStatus(userAuthStatusEnum.getCode());
                break;
            case EMAIL_STATUS:
                entity.setEmailStatus(userAuthStatusEnum.getCode());
                break;
            case IS_OLD_USER:
                entity.setIsOldUser(userAuthStatusEnum.getCode());
                break;
            case FIRST_RISK_STATUS:
                entity.setFirstRoundRiskStatus(userAuthStatusEnum.getCode());
                break;
        }
        userAuthStatusDao.updateByPrimaryKey(entity);
        return Boolean.TRUE;
    }

    @Override
    public UserAuthStatusRO getUserAuthStatus(String userCode) {
        UserAuthStatusEntity userAuthStatusEntity = userAuthStatusDao.getUserAuthStatusByUserCode(userCode);
        if(userAuthStatusEntity == null){
            return null;
        }
        UserAuthStatusRO userAuthStatusRO = new UserAuthStatusRO();
        BeanUtils.copyProperties(userAuthStatusEntity, userAuthStatusRO);
        return userAuthStatusRO;
    }
}
